package com.example.foodclient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.foodclient.pojo.LoginBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private EditText phoneEt;
    private EditText pwdEt;
    private JSONObject jsonObject;
    private Button cancelBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initEvent() {
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {

        phoneEt = findViewById(R.id.phone_et);
        pwdEt = findViewById(R.id.pwd_et);

        cancelBtn = findViewById(R.id.cancel_btn);

    }

    public void login(View view) {
        final String phone = phoneEt.getText().toString();
        final String password = pwdEt.getText().toString();

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient().newBuilder()
                            .build();
                    Request request = new Request.Builder()
                            //请求参数的值一定不能有空格 否则影响参数值
                            .url("http://10.2.64.75:8080/login1_get?telephone=" + phone + "&password=" + password)
                            .method("GET", null)
                            .build();

                    Response response = client.newCall(request).execute();
                    String result = response.body().string();
                    Log.e(TAG, "login: " + result);
                    jsonObject = new JSONObject(result);
                    final boolean success = jsonObject.getBoolean("success");
                    Gson gson = new Gson();
                    LoginBean loginBean = gson.fromJson(result, LoginBean.class);
                    final Integer id = loginBean.getData().getId();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (success) {
                                toFoodListActivity(id);
                            } else {
                                Toast.makeText(MainActivity.this, "手机号或密码不正确", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }).start();

    }


    //跳转到食物列表页
    public void toFoodListActivity(int id) {
        Intent intent = new Intent(MainActivity.this, FoodListActivity.class);
        intent.putExtra("userId",id);
        startActivity(intent);
    }

    //跳转到注册页
    public void toRegister(View view) {
        Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
        startActivity(intent);
    }
}
